<?php
#####################################
# * @Author: lehieu008              #
# * @Email: lehieu008@gmail.com     #
# * @Copyright: 2008 - 2010         #
#####################################
if(!defined('BASEPATH'))exit('No direct script access allowed');
class Sort_module extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index($sorts = array(), $default = '')
    {
        $url = base_url().$this->uri->segment(1).'/';
        #BEGIN: getVar
        switch(trim(strtoupper($this->uri->segment(1))))
        {
        	case 'MEMBER':
        		$segment = 3;
        		$url .= $this->uri->segment(2).'/';
        		break;
        	case 'ACCOUNT':
        		switch(trim(strtoupper($this->uri->segment(3))))
        		{
        			case 'FAVORITE':
        			case 'RECEIVED':
        			case 'SEND':
        			case 'INVITE':
        				$segment = 4;
        				$url .= $this->uri->segment(2).'/'.$this->uri->segment(3).'/';
        				break;
        			default:
        				$segment = 3;
        				$url .= $this->uri->segment(2).'/';
        		}
        		break;
        	default:
        		switch(trim(strtoupper($this->uri->segment(2))))
        		{
        			case 'SEARCH':
        				$segment = 3;
        				$url .= $this->uri->segment(2).'/';
        				break;
        			default:
        				$segment = 2;
        		}
        }
        $segments = array('section', 'category', 'name', 'title', 'hot', 'bestShop', 'vip', 'reliable', 'saleOff', 'fromPrice', 'toPrice', 'fromSalary', 'toSalary', 'currency', 'unitSalary', 'fromView', 'toView', 'address', 'province', 'fromDate', 'toDate', 'search', 'keyword', 'sort', 'by', 'page');
        $getVar = $this->uri->uri_to_assoc($segment, $segments);
        #END getVar
        #BEGIN: Section
        if(isset($getVar['section']) && $getVar['section'] !== FALSE && (int)$getVar['section'] > 0)
        {
        	$url .= 'section/'.$getVar['section'].'/';
        }
        #END Section
        #BEGIN: Category
        if(isset($getVar['category']) && $getVar['category'] !== FALSE && (int)$getVar['category'] > 0)
        {
        	$url .= 'category/'.$getVar['category'].'/';
        }
        #END Category
        #BEGIN: Name || Title
        if(isset($getVar['name']) && $getVar['name'] !== FALSE && trim($getVar['name']) != '')
        {
        	$url .= 'name/'.$getVar['name'].'/';
        }
        elseif(isset($getVar['title']) && $getVar['title'] !== FALSE && trim($getVar['title']) != '')
        {
        	$url .= 'title/'.$getVar['title'].'/';
        }
        #END Name || Title
        #BEGIN: Hot
        if(isset($getVar['hot']) && $getVar['hot'] !== FALSE && (int)$getVar['hot'] == 1)
        {
        	$url .= 'hot/'.$getVar['hot'].'/';
        }
        #END Hot
        #BEGIN: Best Shop
        if(isset($getVar['bestShop']) && $getVar['bestShop'] !== FALSE && (int)$getVar['bestShop'] == 1)
        {
        	$url .= 'bestShop/'.$getVar['bestShop'].'/';
        }
        #END Best Shop
        #BEGIN: Vip
        if(isset($getVar['vip']) && $getVar['vip'] !== FALSE && (int)$getVar['vip'] == 1)
        {
        	$url .= 'vip/'.$getVar['vip'].'/';
        }
        #END Vip
        #BEGIN: Reliable
        if(isset($getVar['reliable']) && $getVar['reliable'] !== FALSE && (int)$getVar['reliable'] == 1)
        {
        	$url .= 'reliable/'.$getVar['reliable'].'/';
        }
        #END Reliable
        #BEGIN: SaleOff
        if(isset($getVar['saleOff']) && $getVar['saleOff'] !== FALSE && (int)$getVar['saleOff'] == 1)
        {
        	$url .= 'saleOff/'.$getVar['saleOff'].'/';
        }
        #END SaleOff
        #BEGIN: From Price
        if(isset($getVar['fromPrice']) && $getVar['fromPrice'] !== FALSE && trim($getVar['fromPrice']) != '')
        {
        	$url .= 'fromPrice/'.$getVar['fromPrice'].'/';
        }
        #END From Price
        #BEGIN: To Price
        if(isset($getVar['toPrice']) && $getVar['toPrice'] !== FALSE && trim($getVar['toPrice']) != '')
        {
        	$url .= 'toPrice/'.$getVar['toPrice'].'/';
        }
        #END To Price
        #BEGIN: From Salary
        if(isset($getVar['fromSalary']) && $getVar['fromSalary'] !== FALSE && trim($getVar['fromSalary']) != '')
        {
        	$url .= 'fromSalary/'.$getVar['fromSalary'].'/';
        }
        #END From Salary
        #BEGIN: To Salary
        if(isset($getVar['toSalary']) && $getVar['toSalary'] !== FALSE && trim($getVar['toSalary']) != '')
        {
        	$url .= 'toSalary/'.$getVar['toSalary'].'/';
        }
        #END To Salary
        #BEGIN: Currency
        if(isset($getVar['currency']) && $getVar['currency'] !== FALSE && (trim(strtoupper($getVar['currency'])) == 'VND' || trim(strtoupper($getVar['currency'])) == 'USD'))
        {
        	$url .= 'currency/'.$getVar['currency'].'/';
        }
        #END Currency
        #BEGIN: Unit Salary
        if(isset($getVar['unitSalary']) && $getVar['unitSalary'] !== FALSE && ((int)$getVar['unitSalary'] == 1 || (int)$getVar['unitSalary'] == 2 || (int)$getVar['unitSalary'] == 3))
        {
        	$url .= 'unitSalary/'.$getVar['unitSalary'].'/';
        }
        #END Unit Salary
        #BEGIN: From View
        if(isset($getVar['fromView']) && $getVar['fromView'] !== FALSE && (int)$getVar['fromView'] >= 0)
        {
        	$url .= 'fromView/'.$getVar['fromView'].'/';
        }
        #END From View
        #BEGIN: To View
        if(isset($getVar['toView']) && $getVar['toView'] !== FALSE && (int)$getVar['toView'] >= 0)
        {
        	$url .= 'toView/'.$getVar['toView'].'/';
        }
        #END To View
        #BEGIN: Address
        if(isset($getVar['address']) && $getVar['address'] !== FALSE && trim($getVar['address']) != '')
        {
        	$url .= 'address/'.$getVar['address'].'/';
        }
        #END Address
        #BEGIN: Province
        if(isset($getVar['province']) && $getVar['province'] !== FALSE && (int)$getVar['province'] >= 0)
        {
        	$url .= 'province/'.$getVar['province'].'/';
        }
        #END Province
        #BEGIN: From Date
        if(isset($getVar['fromDate']) && $getVar['fromDate'] !== FALSE && trim($getVar['fromDate']) != '')
        {
        	$url .= 'fromDate/'.$getVar['fromDate'].'/';
        }
        #END From Date
        #BEGIN: To Date
        if(isset($getVar['toDate']) && $getVar['toDate'] !== FALSE && trim($getVar['toDate']) != '')
        {
        	$url .= 'toDate/'.$getVar['toDate'].'/';
        }
        #END To Date
        #BEGIN: Search
        if(isset($getVar['search']) && $getVar['search'] !== FALSE && trim($getVar['search']) != '' && isset($getVar['keyword']) && $getVar['keyword'] !== FALSE && trim($getVar['keyword']) != '')
        {
        	$url .= 'search/'.$getVar['search'].'/keyword/'.$getVar['keyword'].'/';
        }
        #END Search
        #BEGIN: Sorted
        if(isset($getVar['sort']) && $getVar['sort'] !== FALSE && trim($getVar['sort']) != '' && isset($getVar['by']) && $getVar['by'] !== FALSE && (trim(strtoupper($getVar['by'])) == 'ASC' || trim(strtoupper($getVar['by'])) == 'DESC'))
        {
            foreach($sorts as $sort=>$by)
            {
                if(trim(strtoupper($getVar['sort'])) == trim(strtoupper($sort)))
                {
                    if(trim(strtoupper($getVar['by'])) == 'ASC')
                    {
                        $sorted = $by[0];
                    }
                    else
                    {
                        $sorted = $by[1];
                    }
                    exit();
                }
            }
        }
        if(!isset($data['sorted']))
        {
            $sorted = $default;
        }
        #END Sorted
        $data['sorts'] = $sorts;
        $data['sorted'] = $sorted;
        $data['url'] = $url;
        #Load View
        $this->load->view('sort/defaults', $data);
    }
}